﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;

namespace Ferpect.ComponentModel.Design
{
    public class ParticlesSpriteDesigner : SpriteDesigner
    {
        private Dictionary<string, object> defaultValues;
        private bool initialized;

        public ParticlesSpriteDesigner()
        {
            string[] configuredProperties = new string[]
            {
                "MinInitialSpeed",
                "MaxInitialSpeed",
                "MinAcceleration",
                "MaxAcceleration",
                "MinLifetime",
                "MaxLifetime",
                "MinScale",
                "MaxScale",
                "MinNumParticles",
                "MaxNumParticles",
                "MinRotationSpeed",
                "MaxRotationSpeed",
                "BlendState",
            };

            this.defaultValues = new Dictionary<string, object>();
            foreach (string property in configuredProperties)
            {
                this.defaultValues[property] = null;
            }
        }

        public override void Initialize(System.ComponentModel.IComponent component)
        {
            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(component, true))
            {
                if (this.defaultValues.ContainsKey(property.Name))
                {
                    this.defaultValues[property.Name] = property.GetValue(component);
                }
            }
            this.initialized = true;
            base.Initialize(component);
            TypeDescriptor.Refresh(this.Component);
        }

        public override void InitializeNewComponent(System.Collections.IDictionary defaultValues)
        {
            base.InitializeNewComponent(defaultValues);
        }

        public override void InitializeExistingComponent(System.Collections.IDictionary defaultValues)
        {
            base.InitializeExistingComponent(defaultValues);
        }

        protected override void PostFilterProperties(System.Collections.IDictionary properties)
        {
            if (this.initialized)
            {
                List<PropertyDescriptor> replacements = new List<PropertyDescriptor>();
                foreach (PropertyDescriptor property in properties.Values)
                {
                    if (this.defaultValues.ContainsKey(property.Name))
                    {
                        Debug.Assert(this.defaultValues[property.Name] != null);
                        replacements.Add(TypeDescriptor.CreateProperty(this.Component.GetType(), property, new DefaultValueAttribute(this.defaultValues[property.Name])));
                    }
                }
                foreach (PropertyDescriptor replacement in replacements)
                {
                    properties[replacement.Name] = replacement;
                }
            }
            base.PostFilterProperties(properties);
        }
    }
}
